package com.yklx.youke.admin.mapper;

import com.yklx.youke.entity.Menu;
import org.apache.ibatis.annotations.*;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface MenuMapper {
    /**
     * 获取所有菜单
     * @return 所有菜单列表
     */
    @Select("SELECT menu_id, menu_name, menu_icon, menu_url, parent_id, sort_order, is_active, created_at, updated_at FROM menus ORDER BY parent_id, sort_order")
    @Results(id = "MenuResultMap", value = {
        @Result(property = "menuId", column = "menu_id"),
        @Result(property = "menuName", column = "menu_name"),
        @Result(property = "menuIcon", column = "menu_icon"),
        @Result(property = "menuUrl", column = "menu_url"),
        @Result(property = "parentId", column = "parent_id"),
        @Result(property = "sortOrder", column = "sort_order"),
        @Result(property = "isActive", column = "is_active"),
        @Result(property = "createdAt", column = "created_at"),
        @Result(property = "updatedAt", column = "updated_at")
    })
    List<Menu> getAllMenus();

    /**
     * 根据ID获取菜单
     * @param menuId 菜单ID
     * @return 菜单对象
     */
    @Select("SELECT * FROM menus WHERE menu_id = #{menuId}")
    Menu getMenuById(@Param("menuId") String menuId);

    /**
     * 插入新菜单
     * @param menu 菜单对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO menus (menu_id, menu_name, menu_icon, menu_url, parent_id, sort_order, is_active) " +
            "VALUES (#{menuId}, #{menuName}, #{menuIcon}, #{menuUrl}, #{parentId}, #{sortOrder}, #{isActive})")
    int insertMenu(Menu menu);

    /**
     * 更新菜单
     * @param menu 菜单对象
     * @return 影响的行数
     */
    @Update("UPDATE menus SET menu_name=#{menuName}, menu_icon=#{menuIcon}, menu_url=#{menuUrl}, " +
            "parent_id=#{parentId}, sort_order=#{sortOrder}, is_active=#{isActive} " +
            "WHERE menu_id=#{menuId}")
    int updateMenu(Menu menu);

    /**
     * 删除菜单
     * @param menuId 菜单ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM menus WHERE menu_id = #{menuId}")
    int deleteMenu(@Param("menuId") String menuId);
}